﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Forms;
namespace GameHelper
{
    public class GameHelper
    {
        public static List<WindowAPI.Skill> skills = null;
        public static KeysConverter k = null;
        static List<WindowAPI.GuaiwuExistPoint> guaiwupoints = null;
        static List<WindowAPI.NeedAddBooldPoint> needaddbooldpoints = null;
        public GameHelper()
        {
            skills = WindowAPI.Skill.GetAllSkill();
            guaiwupoints = new WindowAPI.GuaiwuExistPoint().GetList();
            needaddbooldpoints = new WindowAPI.NeedAddBooldPoint().GetList();
            k = new KeysConverter();
        }

        public static int AllKillCount = 0;
        /// <summary>
        /// 攻击
        /// </summary>
        public static void Attack(ref int attackCount, int runTime)
        {
            foreach (WindowAPI.Skill skill in skills)
            {
                //   if (runTime % skill.CoolingTime == 0)
                //    {
                WindowAPI.ApiHelper.KeyBoardClick((Keys)k.ConvertFromString(skill.Key));
                //   }
            }
            WindowAPI.ApiHelper.KeyBoardClick(Keys.Space);
            WindowAPI.ApiHelper.KeyBoardClick(Keys.Z);

            if (attackCount > 20)//打了20下还没有打死
            {
                ChangeToAttack(ref attackCount);
                attackCount = 0;
            }
        }
        /// <summary>
        /// 换一个怪物攻击
        /// </summary>
        public static void ChangeToAttack(ref int attackCount)
        {
            WindowAPI.ApiHelper.KeyBoardClick(Keys.Tab);//换一个怪物打打看看
            attackCount = 0;
        }

        /// <summary>
        /// 检查角色血量是否足够
        /// </summary>
        /// <param name="img"></param>
        /// <returns></returns>
        public static bool BloodIsMany(System.Drawing.Bitmap img)
        {
            bool isBloodMany = true;

            List<Color> colors = new List<Color>();
            foreach (WindowAPI.NeedAddBooldPoint point in needaddbooldpoints)
            {
                Color c = img.GetPixel(point.X, point.Y);
                if (c.R != point.R || c.G != point.G || c.B != point.B)
                {
                    isBloodMany = false;
                    break;
                }
            }

            //Color pixelColor1 = img.GetPixel(262, 51);
            //Color pixelColor2 = img.GetPixel(261, 54);
            //Color pixelColor3 = img.GetPixel(251, 56);
            //Color pixelColor5 = img.GetPixel(253, 51);


            ////(pixelColor1.R + "  " + pixelColor1.G + "  " + pixelColor1.B).Debug();//139  19  6
            ////(pixelColor2.R + "  " + pixelColor2.G + "  " + pixelColor2.B).Debug();//116  22  18
            ////(pixelColor3.R + "  " + pixelColor3.G + "  " + pixelColor3.B).Debug();//146  28  28
            ////(pixelColor5.R + "  " + pixelColor5.G + "  " + pixelColor5.B).Debug();//126  17  6

            //if (pixelColor1.R == 139 && pixelColor1.G == 19 && pixelColor1.B == 6 && pixelColor2.R == 116 && pixelColor2.G == 22 && pixelColor2.B == 18
            //   && pixelColor3.R == 146 && pixelColor3.G == 28 && pixelColor3.B == 28 && pixelColor5.R == 126 && pixelColor5.G == 17 && pixelColor5.B == 6)
            //{
            //    isBloodMany = true;
            //}

            return isBloodMany;
        }



        /// <summary>
        /// 判断当前是否有锁定的怪物
        /// </summary>
        /// <returns></returns>
        public static bool HasGuaiToAttack(System.Drawing.Bitmap img)
        {
            bool hasGuai = false;

            List<Color> colors = new List<Color>();
            foreach (WindowAPI.GuaiwuExistPoint point in guaiwupoints)
            {
                Color c = img.GetPixel(point.X, point.Y);
                if (c.R != point.R || c.G != point.G || c.B != point.B)
                {
                    hasGuai = false;
                    break;
                }
            }

            //Color pixelColor1 = img.GetPixel(366, 39);
            //Color pixelColor2 = img.GetPixel(373, 50);
            //Color pixelColor3 = img.GetPixel(385, 59);
            //Color pixelColor5 = img.GetPixel(418, 41);

            ////(pixelColor1.R + "  " + pixelColor1.G + "  " + pixelColor1.B).Debug();//97  84  58
            ////(pixelColor2.R + "  " + pixelColor2.G + "  " + pixelColor2.B).Debug();//86  74  51
            ////(pixelColor3.R + "  " + pixelColor3.G + "  " + pixelColor3.B).Debug();//72  53  33
            ////(pixelColor5.R + "  " + pixelColor5.G + "  " + pixelColor5.B).Debug();//88  75  51

            //if (pixelColor1.R == 97 && pixelColor1.G == 84 && pixelColor1.B == 58 && pixelColor2.R == 86 && pixelColor2.G == 74 && pixelColor2.B == 51
            //    && pixelColor3.R == 72 && pixelColor3.G == 53 && pixelColor3.B == 33 && pixelColor5.R == 88 && pixelColor5.G == 75 && pixelColor5.B == 51)
            //{
            //    hasGuai = true;
            //}


            return hasGuai;
        }


        public static void InsertBloodConfig(string skill1, int time1, string skill2, int time2, string skill3, int time3, string skill4, int time4)
        {
            WindowAPI.Medicine skill = new WindowAPI.Medicine();
            skill.DeleteAll();

            skill.CoolingTime = time1;
            skill.Key = skill1;
            if (!string.IsNullOrEmpty(skill.Key))
            {
                skill.Insert();
            }

            skill = new WindowAPI.Medicine();
            skill.CoolingTime = time2;
            skill.Key = skill2;
            if (!string.IsNullOrEmpty(skill.Key))
            {
                skill.Insert();
            }

            skill = new WindowAPI.Medicine();
            skill.CoolingTime = time3;
            skill.Key = skill3;
            if (!string.IsNullOrEmpty(skill.Key))
            {
                skill.Insert();
            }


            skill = new WindowAPI.Medicine();
            skill.CoolingTime = time4;
            skill.Key = skill4;
            if (!string.IsNullOrEmpty(skill.Key))
            {
                skill.Insert();
            }

            skill = null;
        }


        public static void InsertSkillConfig(string skill1, int time1, string skill2, int time2, string skill3, int time3, string skill4, int time4, string skill5, int time5)
        {
            WindowAPI.Skill skill = new WindowAPI.Skill();
            skill.DeleteAll();

            skill.CoolingTime = time1;
            skill.Key = skill1;
            if (!string.IsNullOrEmpty(skill.Key))
            {
                skill.Insert();
            }


            skill = new WindowAPI.Skill();
            skill.CoolingTime = time2;
            skill.Key = skill2;
            if (!string.IsNullOrEmpty(skill.Key))
            {
                skill.Insert();
            }

            skill = new WindowAPI.Skill();
            skill.CoolingTime = time3;
            skill.Key = skill3;
            if (!string.IsNullOrEmpty(skill.Key))
            {
                skill.Insert();
            }


            skill = new WindowAPI.Skill();
            skill.CoolingTime = time4;
            skill.Key = skill4;
            if (!string.IsNullOrEmpty(skill.Key))
            {
                skill.Insert();
            }

            skill = new WindowAPI.Skill();
            skill.CoolingTime = time5;
            skill.Key = skill5;
            if (!string.IsNullOrEmpty(skill.Key))
            {
                skill.Insert();
            }

            skill = null;
        }
    }
}
